//
//  FinderDetailsViewController.m
//  InSuger
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 Dior_3g. All rights reserved.
//

#import "FinderDetailsViewController.h"
#import "DetailsModel.h"
#import "CarouselFigureView.h"

@interface FinderDetailsViewController ()<UIWebViewDelegate>
@property(nonatomic, strong) CarouselFigureView *myScrollView;
@property(nonatomic, strong) DetailsModel *myData;
@property(nonatomic, strong) NSMutableArray *imageArr;
@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) UIWebView *webView;
@property(nonatomic, strong) UILabel *descLabel;
@property(nonatomic, strong) UILabel *nameLabel;
@property(nonatomic, strong) UILabel *priceLabel;
@end

@implementation FinderDetailsViewController
{
    UIActivityIndicatorView *acView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"热门详情";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    
    self.navigationController.navigationBar.translucent = NO;// 不透明
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self scrollViewcreateData];
    [self createData];

    [self createWebView];
    

}



#pragma mark scrollView
- (void)scrollViewcreateData
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:self.scrollView];
}

#pragma mark Data
- (void)createData
{
    NSLog(@"%@", self.str);
    [AFN getdataWithString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/items/%@", self.str] block:^(id block) {
         NSDictionary *dic = block;
        self.myData = [[DetailsModel alloc] init];
        [self.myData setValuesForKeysWithDictionary:dic[@"data"]];
        self.imageArr = [NSMutableArray array];
        self.imageArr = [self.myData image_urls];
        [self createWebView];
        [self myScrollViewCreateData];
     
    }];
}

#pragma mark myScrollViewCreateData

- (void)myScrollViewCreateData
{
    self.myScrollView = [[CarouselFigureView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 27 * HEIGHT / 41) withArray:self.imageArr];
    [self.scrollView addSubview:self.myScrollView];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 90, 26 * HEIGHT / 41, WIDTH, 5 * HEIGHT / 41)];
    self.nameLabel.text = [self.myData name];
    [self.scrollView addSubview:self.nameLabel];

    self.priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 90, 28 * HEIGHT / 41, WIDTH, 5 * HEIGHT / 41)];
    self.priceLabel.text = [NSString stringWithFormat:@"🐑%@", [self.myData price]];
    self.priceLabel.textColor = [UIColor redColor];
    self.priceLabel.font = [UIFont systemFontOfSize:18.0];
    [self.scrollView addSubview:self.priceLabel];

    self.descLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 90, 32 * HEIGHT / 41, WIDTH, 11 * HEIGHT / 41)];
    self.descLabel.text = [self.myData myDescription];
    self.descLabel.font = [UIFont systemFontOfSize:13.0];
    self.descLabel.textColor = [UIColor grayColor];
    self.descLabel.numberOfLines = 0;
    [self.descLabel sizeToFit];
    [self.scrollView addSubview:self.descLabel];
    
    @weakify(self);
    [self addColorChangedBlock:^{
        @strongify(self);
        self.scrollView.nightBackgroundColor = UIColorFromRGB(0x343434);
        self.navigationController.navigationBar.nightBarTintColor = UIColorFromRGB(0x444444);
        self.navigationController.navigationBar.normalBarTintColor = [UIColor redColor];
        self.descLabel.nightTextColor = [UIColor whiteColor];
        self.descLabel.normalTextColor = [UIColor grayColor];
        self.nameLabel.nightTextColor = [UIColor whiteColor];
        
    }];

}


#pragma mark WebView Setting
- (void)createWebView
{
    self.scrollView.contentSize = CGSizeMake(0, 2 * HEIGHT);
    self.scrollView.showsVerticalScrollIndicator = YES;
    self.scrollView.pagingEnabled = NO;
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, self.descLabel.frame.size.height + 32 * HEIGHT / 41 + HEIGHT / 8, WIDTH, HEIGHT )];
    [self.scrollView addSubview:self.webView];
    self.webView.delegate  = self;
    self.webView.scalesPageToFit = YES;
    
    // 风火轮
    acView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 32.0f, 32.0f)];
    acView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    
    acView.center = self.view.center;
    [self.view addSubview:acView];
    NSLog(@"%@", [self.myData detail_html]);
    [self.webView loadHTMLString:[self.myData detail_html] baseURL:nil];
}

#pragma mark WebView Delegate Method
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [acView startAnimating];
}


#pragma mark 修改UIWebView中字体的大小
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [acView stopAnimating];
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    // 忽略异步加载返回 -999
    if ([error code] == NSURLErrorCancelled) return;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
